//
//  ClubSafeViewController.swift
//  77Club
//
//  Created by Apple on 2024/9/20.
//

import UIKit

class ClubSafeViewController: ClubBaseViewController {
    
    private let bgImageView = c_make_ex(UIImageView.self)
        .image("Mine_safe_bg").end()
    private let backButton = c_make_ex(UIButton.self)
        .image("Common_blackBack").end()
    private let logoImageView = c_make_ex(UIImageView.self)
        .image("Mine_safe_logo").end()
    private let titleImageView = c_make_ex(UIImageView.self)
        .image("Mine_safe_title").end()
    private let safeImageView = c_make_ex(UIImageView.self)
        .image("Mine_safe_safe").end()
    private let safeLaebl = c_make_ex(UILabel.self)
        .color("#47494D").font(12.font_medium)
        .text("专业可信赖，守护美好乐园").end()
    private let topBgImageV = c_make_ex(UIView.self)
        .bgColor(.white.alpha(0.6)).corner(12)
        .border(.white, width: 1).end()
    private let topTitleLaebl = c_make_ex(UILabel.self)
        .color("#2E3033").font(16.font_semiBold)
        .text("账号").end()
    private let accountLaebl = c_make_ex(UILabel.self)
        .color("#7B7D80").font(14.font_medium).alignment(.center)
        .text("账号关联").end()
    private var accountValueLaebl = c_make_ex(UILabel.self)
        .color("#1E94FF").font(24.font_medium).alignment(.center).end()
    private let levelLaebl = c_make_ex(UILabel.self)
        .color("#7B7D80").font(14.font_medium).alignment(.center)
        .text("安全等级").end()
    private let levelValueLaebl = c_make_ex(UILabel.self)
        .color("#1E94FF").font(24.font_medium).alignment(.center).end()
    private let mediumBgImageV = c_make_ex(UIView.self)
        .bgColor(.white.alpha(0.6)).corner(12)
        .border(.white, width: 1).end()
    private let mediumTitleLaebl = c_make_ex(UILabel.self)
        .color("#2E3033").font(16.font_semiBold)
        .text("账号关联").end()
    private let verfityLabel = UIIconButton(style: .leading(itemSpace: 8))
    private let phoneLabel = UIIconButton(style: .leading(itemSpace: 8))
    private let phoneContentLaebl = c_make_ex(UILabel.self)
        .color("#2E3033").font(14.font_medium).alignment(.right).end()
    private let verfityStateLaebl = c_make_ex(UILabel.self)
        .color("#FF7700").font(14.font_medium).alignment(.right)
        .text("去认证").end()
    private let phoneStateLaebl = c_make_ex(UILabel.self)
        .color("#FF7700").font(14.font_medium).alignment(.right)
        .text("已绑定").end()
    private let bottomBgImageV = c_make_ex(UIView.self)
        .bgColor(.white.alpha(0.6)).corner(12)
        .border(.white, width: 1).end()
    private let bottomTitleLaebl = c_make_ex(UILabel.self)
        .color("#2E3033").font(16.font_semiBold)
        .text("平台活动").end()
    private let passwordLabel = UIIconButton(style: .leading(itemSpace: 8))
    private let editLaebl = c_make_ex(UILabel.self)
        .color("#FF7700").font(14.font_medium).alignment(.right)
        .text("修改").end()
    private let verfityButton = c_make_ex(UIButton.self).end()
    private let phoneButton = c_make_ex(UIButton.self).end()
    private let editButton = c_make_ex(UIButton.self).end()
    
    private var isVerfity = false
    private var isBang = false
    
    override func viewDidLoad() {
        super.viewDidLoad()

        loadData()
        
        view.subviews{
            bgImageView
            backButton
            logoImageView
            titleImageView
            safeImageView
            safeLaebl
            topBgImageV.subviews{
                topTitleLaebl
                accountLaebl
                accountValueLaebl
                levelLaebl
                levelValueLaebl
            }
            mediumBgImageV.subviews{
                mediumTitleLaebl
                verfityLabel
                phoneLabel
                phoneContentLaebl
                verfityStateLaebl
                phoneStateLaebl
                verfityButton
                phoneButton
            }
            bottomBgImageV.subviews{
                bottomTitleLaebl
                passwordLabel
                editLaebl
                editButton
            }
        }
        
        bgImageView.fillContainer()
        backButton.leading(0).top(.statusHeight).width(56).height(44)
        logoImageView.trailing(0).top(.statusHeight + 2).width(192.0.Scale).height(180.0.Scale)
        titleImageView.leading(12.0.Scale).top(CGFloat.navi_height + 16).width(143.0.Scale).height(23.0.Scale)
        safeImageView.leading(12.0.Scale).size(12.0.Scale)
        safeImageView.Top == titleImageView.Bottom + 8.0.Scale
        safeLaebl.leading(28.0.Scale)
        safeLaebl.CenterY == safeImageView.CenterY
        topBgImageV.leading(12.0.Scale).trailing(12.0.Scale).height(130.0.Scale)
        topBgImageV.Top == safeImageView.Bottom + 24.0.Scale
        topTitleLaebl.leading(12.0.Scale).top(20.0.Scale)
        let w = (CGFloat.screenWidth - 24.0.Scale)/2
        accountValueLaebl.leading(0).top(60).width(w)
        accountLaebl.leading(0).bottom(24).width(w)
        levelValueLaebl.trailing(0).top(60).width(w)
        levelLaebl.trailing(0).bottom(24).width(w)
        mediumBgImageV.leading(12.0.Scale).trailing(12.0.Scale).height(172.0.Scale)
        mediumBgImageV.Top == topBgImageV.Bottom + 24.0.Scale
        bottomBgImageV.leading(12.0.Scale).trailing(12.0.Scale).height(116.0.Scale)
        bottomBgImageV.Top == mediumBgImageV.Bottom + 24.0.Scale
        mediumTitleLaebl.leading(12.0.Scale).top(20.0.Scale)
        verfityLabel.leading(12.0.Scale).top(60.0.Scale).width(100).height(32)
        phoneLabel.leading(12.0.Scale).top(116.0.Scale).width(105).height(32)
        verfityStateLaebl.trailing(12.0.Scale)
        verfityStateLaebl.CenterY == verfityLabel.CenterY
        phoneStateLaebl.trailing(12.0.Scale)
        phoneStateLaebl.CenterY == phoneLabel.CenterY
        phoneContentLaebl.trailing(62.0.Scale)
        phoneContentLaebl.CenterY == phoneStateLaebl.CenterY
        bottomTitleLaebl.leading(12.0.Scale).top(20.0.Scale)
        passwordLabel.leading(12.0.Scale).top(60.0.Scale).width(100).height(32)
        editLaebl.trailing(12.0.Scale)
        editLaebl.CenterY == passwordLabel.CenterY
        verfityButton.leading(0).trailing(0).height(40)
        verfityButton.CenterY == verfityLabel.CenterY
        phoneButton.leading(0).trailing(0).height(40)
        phoneButton.CenterY == phoneLabel.CenterY
        editButton.leading(0).trailing(0).height(40)
        editButton.CenterY == passwordLabel.CenterY
        
        verfityLabel.style { v in
            v.iconSize = CGSize(width: 32, height: 32)
            v.setImage("Mine_safe_verifty".toImage, for: .normal)
            v.setTitleColor("#2E3033".toColor, for: .normal)
            v.setTitle("实名认证", for: .normal)
            v.titleLabel?.font = 14.font_medium
        }
        phoneLabel.style { v in
            v.iconSize = CGSize(width: 32, height: 32)
            v.setImage("Mine_safe_phone".toImage, for: .normal)
            v.setTitleColor("#2E3033".toColor, for: .normal)
            v.setTitle("绑定手机号", for: .normal)
            v.titleLabel?.font = 14.font_medium
        }
        passwordLabel.style { v in
            v.iconSize = CGSize(width: 32, height: 32)
            v.setImage("Mine_safe_lock".toImage, for: .normal)
            v.setTitleColor("#2E3033".toColor, for: .normal)
            v.setTitle("登录密码", for: .normal)
            v.titleLabel?.font = 14.font_medium
        }
        
        mediumBgImageV.isUserInteractionEnabled = true
        bottomBgImageV.isUserInteractionEnabled = true
        backButton.addTarget(self, action: #selector(clickBackButton), for: .touchUpInside)
        verfityButton.addTarget(self, action: #selector(pushToVerfity), for: .touchUpInside)
        phoneButton.addTarget(self, action: #selector(pushToPhone), for: .touchUpInside)
        editButton.addTarget(self, action: #selector(pushToEditVC), for: .touchUpInside)
    }
    
    
    private func loadData(){
        
        Task {
            let res = await network.request(.safeCenter,type: ClubMineSafeModel.self)
            if res.isSuccess, let model = res.model {
                self.accountValueLaebl.text = "\(model.grade)%"
                self.levelValueLaebl.text = model.dengji
                self.phoneContentLaebl.text = model.phone
                for md in model.nav {
                    if md.type == "0" {
                        //实名认证
                        self.isVerfity = md.status == "已认证"
                        self.verfityStateLaebl.text = md.status
                    }else if md.type == "3" {
                        //绑定手机号
                        self.isBang = md.status == "已绑定"
                        self.phoneStateLaebl.text = md.status
                    }
                }
            }
        }
    }
}

extension ClubSafeViewController {
    
    @objc private func pushToVerfity(){
        if !isVerfity {
            let verfityVC = ClubRealVerfityViewController()
            verfityVC.updateUserInfoData = {
                self.loadData()
            }
            self.navigationController?.pushViewController(verfityVC, animated: true)
        }
    }
    
    @objc private func pushToPhone(){
        if !isBang {
            
        }
    }
    
    @objc private func pushToEditVC(){
        
        self.navigationController?.pushViewController(ClubResetPasswordViewController(), animated: true)
    }
}
